require("../util.js");
require("../component/components.js");
require("../../jsMt/util.js");
var userInfo=JSON.parse(sessionStorage.getItem("userInfo"));
if(!userInfo){
    location.href="/loginIndex"
}
console.log(userInfo.id)
var vm = new Vue({
    el:"#foundCircle",
    data:{
        isClassify:false,
        data:{
            loginUid:userInfo.id,
            categoryId:"",
            circleName:"",
            coverImgSrc:"",
            coverImgWidth:"",
            coverImgHeight:"",
            introduction:"",
            isValidate:""
        },
        img:"",
        imgNum:"1",
        results:{},
        categoryId:"",//分类id
        // uid:"",//用户id
        name:"",// 分类名称
        circleName:"",
        cirleText:"",
        proving:{}
    },
    mounted:function(){
        this.searchObject = this.getQueryStringArgs();
        this.data.categoryId=this.searchObject&&this.searchObject.categoryId;
        this.name=decodeURI(this.searchObject&&this.searchObject.name);
        // this.data.loginUid=this.searchObject&&this.searchObject.loginUid;
        //this.createGroup();
        // console.log(this.data.loginUid)
    },
    methods:{
        //后退
        getBack: function () {
            history.go(-1);
        },
        bgimg:function(url){
            url = url?url:"";
            return {
                "background-image":'url("'+url+'"),url("")'
            }
        },
        /*获取查询字符对象*/
        getQueryStringArgs: function () {
            //取得查询字符串并去掉开头的问号
            var qs = (location.search.length > 0 ? location.search.substring(1) : ""),
                args = {},
                items = qs.length ? qs.split("&") : [],
                item = null,
                name = null,
                value = null,
                len = items.length;
            for (var i = 0; i < len; i++) {
                item = items[i].split("=");
                name = item[0];
                value = item[1];
                if (name.length) {
                    args[name] = value;
                }
            }
            return args;
        },
        getSelect:function(){
            var that=this;
            var data = [{ "id": "0", "value": "任何人可加入" }, { "id": "1", "value": "需经过圈主同意" }];
            var bankSelect = new IosSelect(1,
                [data],
                {
                    container: '.provingMode',
                    itemHeight: 30,
                    itemShowCount: 3,
                    callback: function (selectOneObj) {
                        //console.log(selectOneObj)
                        that.proving=selectOneObj;
                        that.data.isValidate=that.proving.id;
                        console.log(that.proving)
                    }
                });
        },
        getChange:function(){
            var that=this;
            that.data.coverImgSrc=document.getElementById("fileinput").files[0];//$("#fileinput").files[0];
            lrz(that.data.coverImgSrc, function (results) {
                // 你需要的数据都在这里，可以以字符串的形式传送base64给服务端转存为图片。
                console.log(results);
                that.results=results;
                that.imgNum=2;
                demo_report(results.base64);
            });
        },
        //乐友-创建圈子
        createGroup:function(){
            var that=this;
            console.log(that.data)
            var formData = new FormData();
            formData.append("circleName", that.data.circleName);
            formData.append("loginUid", that.data.loginUid);
            formData.append("coverImgWidth",that.data.coverImgWidth);
            formData.append("coverImgHeight", that.data.coverImgHeight);
            formData.append("introduction", that.data.introduction);
            formData.append("isValidate", that.data.isValidate);
            formData.append("categoryId", that.data.categoryId);
            formData.append("coverImgSrc",that.data.coverImgSrc);
            if(!that.data.circleName){
                vm.$refs.prompt.toast("圈子名称不能为空！");
                return;
            }
            if(!that.data.introduction){
                vm.$refs.prompt.toast("圈子简介不能为空！");
                return;
            }
            if(!that.data.coverImgSrc){
                vm.$refs.prompt.toast("圈子头像不能为空！");
                return;
            }
            if(!that.data.isValidate){
                vm.$refs.prompt.toast("成员验证方式不能为空！");
                return;
            }
            if(!that.data.categoryId){
                vm.$refs.prompt.toast("所属频道不能为空！");
                return;
            }
            console.log(formData)
           $.ajax({
                url:host+'/v1/friend/createGroup',   //http://192.168.100.104:8083
                type:'POST',
                data:formData,
                dataType:'json',
                async: false,
                cache: false,
                contentType: false, //不设置内容类型
                processData: false, //不处理数据
                success:function(data){
                    console.log(data);
                    // alert(data.msg);
                    if(data.code==0){
                        location.href="/see/selfMediaPage?categoryId="+that.data.categoryId;
                    }else{
                        vm.$refs.prompt.toast("创建圈子失败！")
                    }
                },
                error:function(){
                    vm.$refs.prompt.toast("创建圈子失败！")
                }
            });
        },
        getNext:function(type){
            var path;
            if(type==1){
                path="/friend/circleMember?circleId="+this.circleId+"&uid="+this.uid+"&isMain="+this.groupInfo.isMain;
            }else if(type==2){
                path="/friend/abstractEdit?circleId="+this.circleId+"&uid="+this.uid;
            }else if(type==3){
                path="/friend/changeChannel?type=2";
            }
            location.href=path;
        }
    },
    watch:{
        // cirleText:function(){
        //     this.data.introduction=encodeURI(this.cirleText)
        // },
        results:function(){
            // var img = new Image();
            // img.onload=function(){
            //     var width = img.width;
            //     var height = img.height;
            //     alert(width+'======'+height);
            // };
        }
    }
});
function demo_report(src) {
    var img = new Image();
    img.onload=function(){
        vm.data.coverImgWidth = img.width;
        vm.data.coverImgHeight = img.height;

    };
    $(".circleSetup-cover").append(img);
    img.src = src;
}